//
//  JKOCRealmSelectModelTests.m
//  JKOCRealmTests
//
//  Created by 王冲 on 2019/2/26.
//  Copyright © 2019年 JK科技有限公司. All rights reserved.
//

#import <XCTest/XCTest.h>
#import <Realm/Realm.h>
#import "Student.h"
@interface JKOCRealmSelectModelTests : XCTestCase

@end

@implementation JKOCRealmSelectModelTests

#pragma mark 查询模型：一旦检索执行之后, RLMResults 将随时保持更新
- (void)testSelectModel{
   
    RLMResults *results = [Student allObjects];
    
    NSLog(@"第 1 次打印结果：%@",results);
    
    Student *student = [[Student alloc]initWithValue:@{@"studentId":@1,@"studentName":@"小王"}];
    
    RLMRealm *realm = [RLMRealm defaultRealm];
    
    [realm transactionWithBlock:^{
        
        [realm addObject:student];
    }];
    NSLog(@"第 2 次打印结果：%@",results);
}
    
#pragma mark 查询方式一：查询某一个模型的查询所有内容
- (void)testSelectModel1{
    
    RLMResults *results = [Student allObjects];
    
    NSLog(@"打印结果：%@",results);
}
    
#pragma mark 查询方式二：条件查询
- (void)testSelectModel2{
    
    RLMResults<Student *> *students = [Student objectsWhere:@"studentName = '小王'"];
    NSLog(@"打印结果：%@",students);
    
}
    
#pragma mark 查询方式三
- (void)testSelectModel3{
    
    RLMResults<Student *> *students = [Student allObjects];
    
     NSLog(@"没有排序的结果：%@",students);
    
    RLMResults *soreStudents = [students sortedResultsUsingKeyPath:@"studentId" ascending:YES];
    
    NSLog(@"排序后的结果：%@",soreStudents);
    
}
    
#pragma mark 查询方式四：链式查询(在查询结果的基础上, 进行二次查询)
- (void)testSelectModel4{
    
    RLMResults<Student *> *students1 = [Student allObjects];
    
    NSLog(@"第 1 次查询的结果：%@",students1);
    
    RLMResults<Student *> *students2 = [Student objectsWhere:@"studentName = '小王'"];
    
    NSLog(@"第 2 次查询的结果：%@",students2);
    
}

#pragma mark 查询方式五：分页
- (void)testSelectModel5{
    
    RLMResults<Student *> *students = [Student allObjects];
    
    for (NSInteger i = 0; i < 2; i++) {
    
        Student *student = students[i];
        NSLog(@"结果是：%@",student);
    }
}
    
#pragma mark 查询方式六：Filtering(过滤)：NSPredicate,以下内容将通过调用[RLMObject objectsWhere:]从默认Realm中检索名称以“小”开头的所有学生来扩展我们之前的示例：
- (void)testSelectModel6{
    
    // Query using a predicate string：使用谓词字符串进行查询
    RLMResults<Student *> *students1 = [Student objectsWhere:@"studentName BEGINSWITH '小'"];
    NSLog(@"students1=%@",students1);
    
    // Query using an NSPredicate：使用NSPredicate查询
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"studentName BEGINSWITH %@",@"小"];
    RLMResults<Student *> *student2 = [Student objectsWithPredicate:pred];
    
    NSLog(@"students2=%@",student2);
    
}

@end
